<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            border-collapse: collapse;
            width: 500px;
        }

        table tr td,
        table tr th {
            border: 1px solid #000;
            text-align: center;
        }
    </style>
</head>

<body>
    <div id="app">
        <table>
            <tr>
                <th>序号</th>
                <th>名称</th>

                <th id="ac" @click="checkSort">价格
                    <span v-show="type && flag">↑</span>
                    <span v-show="type && !flag">↓</span>
                </th>

                <th>数量</th>
                <th>合计</th>
                <th>删除</th>

            </tr>
            <tr v-for="(item,index) in cart">
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>{{item.price}}</td>

                <td><button @click="item.num--" :disabled="item.num<=1">-</button>{{item.num}}
                    <button @click="item.num++">+</button>

                </td>
                <td>{{item.num*item.price}}</td>
                <td><button @click="del(index)">×</button></td>

                <!-- <td><button @click="revise(value)">修改</button></td> -->
            </tr>

        </table>
        <div>总价格：{{sum()}}</div>
    </div>

    <script src="./js/vue.js"></script>
    <script>
        // 创建新的vue  渲染数据不需要获取节点 
        var app = new Vue({
            el: '#app',//将vue挂载在节点上
            data: {
                cart: [
                    {
                        id: 1,
                        name: '小米',
                        price: 1800,
                        num: 1,

                    }, {
                        id: 2,
                        name: '苹果',
                        price: 5400,
                        num: 1,

                    }, {
                        id: 3,
                        name: '华为',
                        price: 2200,
                        num: 1,

                    },
                    {
                        id: 4,
                        name: '荣耀',
                        price: 2100,
                        num: 1,

                    },
                ],
                type: false,
                flag: true
            },
            methods: {
                sum() {
                    console.log(666);
                    var num = 0;
                    this.cart.forEach((item) => {
                        num += item.num * item.price

                    })
                    return num;
                },
                del(index) {
                    this.cart.splice(index, 1)
                },
                checkSort() {
                    this.type = true
                    if (this.flag) {

                        this.cart.sort((a, b) => a.price - b.price);
                        this.flag = false

                    }
                    else {

                        this.cart.sort((a, b) => b.price - a.price);
                        this.flag = true

                    }

                }

            }

        })
    </script>
</body>

</html>